package com.artqiyi.dollmachine.views.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.artqiyi.dollmachine.R;
import com.artqiyi.dollmachine.common.Base.BaseFragment;
import com.artqiyi.dollmachine.model.entity.CoinBuyBean;
import com.artqiyi.dollmachine.presenter.PayPresenter;
import com.artqiyi.dollmachine.presenter.base.PresenterCallbackCollection;
import com.artqiyi.dollmachine.utils.RxRecyclerViewDividerTool;
import com.artqiyi.dollmachine.utils.ScreenUtil;
import com.artqiyi.dollmachine.utils.ViewClickUtil;
import com.artqiyi.dollmachine.utils.manager.PayDialogManager;
import com.artqiyi.dollmachine.utils.manager.UserInfoManager;
import com.artqiyi.dollmachine.views.adapter.PayFragmentAdapter;
import com.artqiyi.dollmachine.views.widget.ToolBarView;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * 充值界面
 */
public class PayFragment extends BaseFragment<PayPresenter> implements View.OnClickListener, PresenterCallbackCollection.PayFragmentCallBack{

    @Bind(R.id.recyclerView)
    RecyclerView mRecyclerView;

    @Bind(R.id.tvPay)
    TextView tvPay;
    @Bind(R.id.tvCoin)
    TextView tvCoin;

    private PayFragmentAdapter mAdapter;

    private List<CoinBuyBean> mCoinBuyBeans = new ArrayList<>();

    private int mSelectPosition = -1;
    @Override
    protected PayPresenter getPresenter() {
        return new PayPresenter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_pay;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        tvPay.setOnClickListener(this);
        mAdapter = new PayFragmentAdapter(mCoinBuyBeans);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.addItemDecoration(new RxRecyclerViewDividerTool(ScreenUtil.dp2px(getActivity(), 5f)));
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                mSelectPosition = position;
                mAdapter.setSelectPosition(position);
                mAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected void handleToolBar(ToolBarView toolBar) {
        super.handleToolBar(toolBar);
        toolBar.setTitleText("我的金币");
    }

    @Override
    public void loadData() {
        super.loadData();
        mPresenter.getCoinBuy();
    }

    @Override
    public void reLoadData() {
        super.reLoadData();
        tvCoin.setText(String.valueOf(UserInfoManager.getInstance().getUserInfoBean().getCoin()));
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvPay:
                if (!ViewClickUtil.canClick()) return;
                if (mSelectPosition != -1) {
                    PayDialogManager.getInstance().getPay(mCoinBuyBeans.get(mSelectPosition));
                } else {
                    Toast.makeText(getActivity(),"请先选择充值选项", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    //获取娃娃币充值列表成功
    @Override
    public void onCoinBuySuccess(List<CoinBuyBean> beans) {
        if (null != beans) {
            mCoinBuyBeans.clear();
            mCoinBuyBeans.addAll(beans);
            mAdapter.notifyDataSetChanged();
        }
    }

    //获取娃娃币充值列表失败
    @Override
    public void onCoinBuyFail(String msg) {
        Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
    }
}
